package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {

        //新增套餐
        setmealMapper.add(setmeal);
        //添加套餐和检查组的中间表数据
        setSetmealAndCheckGroup(checkgroupIds,setmeal.getId());

        if (setmeal.getImg()!=null){
            //将保存到数据库的图片保存到redis的set集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        }

    }

    private void setSetmealAndCheckGroup(Integer[] checkgroupIds, Integer id) {

        if (checkgroupIds!=null && checkgroupIds.length>0){
            setmealMapper.setSetmealAndCheckGroup(checkgroupIds,id);
        }

    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage,pageSize);
        //条件查询数据
        List<Setmeal> setmealList = setmealMapper.findByCondition(queryString);
        //将查询出来的数据通过PageInfo进行包装
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.findAll();
    }

    /**
     * 通过套餐id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {

        //通过套餐id查询套餐信息
        Setmeal setmeal =  setmealMapper.findById(id);

        //通过套餐的id查询检查组的相关信息
        List<CheckGroup> checkGroups = checkGroupMapper.findBySetmealId(setmeal.getId());

        //将查询到的所有的检查组信息封装到套餐中
        setmeal.setCheckGroups(checkGroups);

        //循环遍历每一个检查组
        for (CheckGroup checkGroup : checkGroups) {
            //通过检查组的id查询对应的检查项的集合
            List<CheckItem> checkItems = checkItemMapper.findByCheckGroupId(checkGroup.getId());
            //将所有的检查项的数据封装到每一个对应的检查组中
            checkGroup.setCheckItems(checkItems);
        }

        return setmeal;
    }

    /**
     * 查询套餐占比统计数据
     * @return
     */
    @Override
    public Map getSetmealReport() {

        //定义一个集合，用来存放每一个套餐名称
        List<String> setmealNames = new ArrayList<>();

        List<Map> setmealCounts = setmealMapper.getSetmealReport();
        for (Map map : setmealCounts) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }

        //定义一个map用户封装套餐名称和套餐占比数据
        Map map = new HashMap();
        map.put("setmealNames",setmealNames);
        map.put("setmealCounts",setmealCounts);

        return map;
    }
}
